package com.handle.http;

import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * @author linyu.dai
 * @project rpc
 * @description 文件路由处理
 * @creat 2022/11/29 20:13:38
 */
@Slf4j
public class HttpFileRequestHandle extends HttpBaseHandle{

    public static DefaultFullHttpResponse process(String path){
        //去掉 “/”
        try (InputStream resourceAsStream =
                HttpFileRequestHandle.class.getClassLoader().getResourceAsStream(path.substring(1))){
            if(Objects.isNull(resourceAsStream)){
                throw new RuntimeException("文件不存在");
            }
            byte[] bytes = resourceAsStream.readAllBytes();
            return success(bytes,"");
        } catch (Exception e) {
            log.error("{} 读取文件失败", path);
            return error404();
        }
    }

}
